﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;
using GlxRRSDB.Data;

namespace GlxRRSMain.UserControls
{
    public partial class TicketManagementControl : UserControl
    {
        private CinemaTicketRevenueViewTableAdapter x_CinemaTicketRevenueViewTableAdapter = new CinemaTicketRevenueViewTableAdapter();
        private List<ReportDataSet.CinemaTicketRevenueViewRow> x_ListCinemaTicketRevenue = null;
        ReportDataSet.CinemaTicketRevenueViewRow x_CinemaTicketRevenue = null;
        //
        private tblCinemaTableAdapter x_tblCinemaTableAdapter = new tblCinemaTableAdapter();
        private List<ReportDataSet.tblCinemaRow> x_ListCinema = null;
        //
        private tblTicketRevenueTableAdapter x_tblTicketRevenueTableAdapter = new tblTicketRevenueTableAdapter();

        //
        private void TicketManagementControl_Load(object sender, EventArgs e)
        {
            initDataGridView();
            //
            gridTicketRevenue.DataSource = ListCinemaTicketRevenue;
            //
            cbCinema.DataSource = ListCinema;
            cbCinema.DisplayMember = "CinemaName";
        }
        public TicketManagementControl()
        {
            InitializeComponent();
        }
        private void initDataGridView()
        {
            initTicketRevenueDataGridView();
        }
        private void initTicketRevenueDataGridView()
        {
            gridTicketRevenue.AutoGenerateColumns = false;
            gridTicketRevenue.ReadOnly = true;
            DataGridViewTextBoxColumn c00 = null;
            //
            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "ID";
            c00.HeaderText = " Ticket Revenue ID";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "CinemaName";
            c00.HeaderText = "Cinema Name";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "Adult2D";
            c00.HeaderText = "Adult 2D";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "Child2D";
            c00.HeaderText = "Child 2D";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "Senior2D";
            c00.HeaderText = "Senior 2D";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "Student2D";
            c00.HeaderText = "Student 2D";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "CompTotal2D";
            c00.HeaderText = "Comp Total 2D";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "Revenue2D";
            c00.HeaderText = "Revenue 2D";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "Standard3D";
            c00.HeaderText = "Standard 3D";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "VIP3D";
            c00.HeaderText = "VIP 3D";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "Revenue3D";
            c00.HeaderText = "Revenue3D";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "RevenueConcession";
            c00.HeaderText = "RevenueConcession";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "Trans";
            c00.HeaderText = "Trans";
            gridTicketRevenue.Columns.Add(c00);

            c00 = new DataGridViewTextBoxColumn();
            c00.DataPropertyName = "Date";
            c00.HeaderText = "Date";
            gridTicketRevenue.Columns.Add(c00);
        }

        private void bindingTicketRevenueData()
        {
            if (CurrentCinemaTicketRevenue != null)
            {
                txtTicketRevenueID.Text = CurrentCinemaTicketRevenue.ID.ToString();
                cbCinema.Text = CurrentCinemaTicketRevenue.CinemaName;
                txtAdult2D.Text = CurrentCinemaTicketRevenue.Adult2D.ToString();
                txtChild2D.Text = CurrentCinemaTicketRevenue.Child2D.ToString();
                txtSenior2D.Text = CurrentCinemaTicketRevenue.Senior2D.ToString();
                txtStudent2D.Text = CurrentCinemaTicketRevenue.Student2D.ToString();
                txtCompTotal2D.Text = CurrentCinemaTicketRevenue.CompTotal2D.ToString();
                txtRevenue2D.Text = CurrentCinemaTicketRevenue.Revenue2D.ToString();
                //
                txtStandard3D.Text = CurrentCinemaTicketRevenue.Standard3D.ToString();
                txtVip3D.Text = CurrentCinemaTicketRevenue.VIP3D.ToString();
                txtRevenue3D.Text = CurrentCinemaTicketRevenue.Revenue3D.ToString();
                //
                txtRevenueConsession.Text = CurrentCinemaTicketRevenue.RevenueConcession.ToString();
                txtTrans.Text = CurrentCinemaTicketRevenue.Trans.ToString();
                dpickerDateTicketRevenue.Value = CurrentCinemaTicketRevenue.Date;
            }
        }

        public List<ReportDataSet.CinemaTicketRevenueViewRow> ListCinemaTicketRevenue
        {
            get
            {
                if (x_ListCinemaTicketRevenue == null)
                {
                    x_ListCinemaTicketRevenue = x_CinemaTicketRevenueViewTableAdapter.GetData().ToList();
                }
                return x_ListCinemaTicketRevenue;
            }
            set
            {
                x_ListCinemaTicketRevenue = value;
            }
        }

        public ReportDataSet.CinemaTicketRevenueViewRow CurrentCinemaTicketRevenue
        {
            get
            {
                if (x_CinemaTicketRevenue == null)
                {
                    if (gridTicketRevenue.CurrentRow != null)
                    {
                        x_CinemaTicketRevenue = ListCinemaTicketRevenue[gridTicketRevenue.CurrentRow.Index];
                    }
                }
                return x_CinemaTicketRevenue;
            }
            set
            {
                x_CinemaTicketRevenue = value;
            }
        }

        public List<ReportDataSet.tblCinemaRow> ListCinema
        {
            get
            {
                if (x_ListCinema == null)
                {
                    x_ListCinema = x_tblCinemaTableAdapter.GetData().ToList();
                }
                return x_ListCinema;
            }
            set
            {
                x_ListCinema = value;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            dpickerDateTicketRevenue.Value = DateTime.Now;
        }

        private void gridTicketRevenue_SelectionChanged(object sender, EventArgs e)
        {
            x_CinemaTicketRevenue = null;
            bindingTicketRevenueData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
            //
            ImportTicketTypeData data = new ImportTicketTypeData();
            data.CinemaID = CinemaID;
            data.Adult2D = int.Parse(txtAdult2D.Text);
            data.Child2D = int.Parse(txtChild2D.Text);
            data.Senior2D = int.Parse(txtSenior2D.Text);
            data.Student2D = int.Parse(txtStudent2D.Text);
            data.CompTotal2D = int.Parse(txtCompTotal2D.Text);
            data.Revenue2D = int.Parse(txtRevenue2D.Text);
            data.Revenue2D = int.Parse(txtRevenue2D.Text);
            data.Standard3D = int.Parse(txtStandard3D.Text);
            data.VIP3D = int.Parse(txtVip3D.Text);
            data.Revenue3D = int.Parse(txtRevenue3D.Text);
            data.Trans = int.Parse(txtTrans.Text);
            data.RevenueConcession = int.Parse(txtRevenueConsession.Text);
            data.Date = dpickerDateTicketRevenue.Value;
            //
            x_tblTicketRevenueTableAdapter.Insert(data);

            ListCinemaTicketRevenue = null;
            gridTicketRevenue.DataSource = ListCinemaTicketRevenue;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int ticketId = int.Parse(txtTicketRevenueID.Text);
            //
            ReportDataSet.tblTicketRevenueRow current = x_tblTicketRevenueTableAdapter.GetData()
                .Where(kvp => kvp.ID == ticketId)
                .First();
            //
            int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
            //
            current.CinemaID = CinemaID;

            //current.ID = int.Parse(txtTicketRevenueID.Text);
            current.Adult2D = int.Parse(txtAdult2D.Text);
            current.Child2D = int.Parse(txtChild2D.Text);
            current.Senior2D = int.Parse(txtSenior2D.Text);
            current.Student2D = int.Parse(txtStudent2D.Text);
            current.CompTotal2D= int.Parse(txtCompTotal2D.Text);
            current.Revenue2D = int.Parse(txtRevenue2D.Text);

            current.Standard3D = int.Parse(txtStandard3D.Text);
            current.VIP3D = int.Parse(txtVip3D.Text);
            current.Revenue3D = int.Parse(txtRevenue3D.Text);
            //
            current.RevenueConcession = int.Parse(txtRevenueConsession.Text);
            current.Trans = int.Parse(txtTrans.Text);
            current.Date = dpickerDateTicketRevenue.Value;

            x_tblTicketRevenueTableAdapter.Update(current);

            ListCinemaTicketRevenue = null;
            gridTicketRevenue.DataSource = ListCinemaTicketRevenue;
        }
    }
}
